package com.cc.utils.excell;

import com.cc.dao.DBTransToExcell;
import com.cc.dao.LibaryProperties;
import com.cc.dao.dto.ShowTableDto;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @User c.c
 * @Since 2022/8/15
 * @Time 13:39
 * @Description 根据用户输入转换成excell中间工具
 */

public class DbTransToExcellUtils {
  /**
   * 设置内容
   * @author C.c
   * @since V1.0   2022-08-15 10:53
   * @param resultSet jdbc读取数据库字段
   * @param columnName 列名
   * @return
   **/
  public static String[][] setContent(String[] columnName, ResultSet resultSet) throws SQLException {
    //得到最后一排
    resultSet.last();
    int row = resultSet.getRow();
    String[][] content = new String[row][columnName.length];

    //得到最前排
    resultSet.beforeFirst();
    //排
    for (int i = 0; i < row; i++) {
      resultSet.next();
      //列
      for (int j = 0; j < columnName.length; j++) {
        //设置每列每排的元素
        content[i][j] = resultSet.getString(columnName[j]);
      }
    }
    return content;
  }

  /**
   * 设置标头
   * @author C.c
   * @since V1.0 2022-08-15 11:03
   * @param element    需要转换成excell的字段
   * @return 列名
   **/
  public static String[] setTitle(List<DBTransToExcell> element){
    List<String> titlelist = element.stream().map(e -> {
      if (e.getExcellColumnName()==null){
        return e.getDbField();
      }
      return e.getExcellColumnName();
    }).collect(Collectors.toList());
    //标题
    String[] title = new String[titlelist.size()];
    int i = 0;
    for (String s : titlelist) {
      title[i++] = s;
    }
    return title;
  }


  /**
   * 获取库下，所有表的"结构"数据
   * @author C.c
   * @since 2022-08-16 16:27
   * @param
   * @return
  **/
  public static List<ShowTableDto> getLibraryAllTableStruct(LibaryProperties libaryProperties, String tableName){
    List<ShowTableDto> tablelist = null;

    try {
      tablelist= libaryProperties.get(tableName).stream().map(e -> {
        return new ShowTableDto(e.getColumnComment(),e.getColumnName(),e.getColumnType(),e.getCharacterMaximumLength(),e.getTableName());
      }).collect(Collectors.toList());
    }catch (Exception e){

      String msg = String.format("数据库:[%s], 不存在表:[%s]", libaryProperties.getTableStructs().get(0).getTableSchema(), tableName);
      throw new RuntimeException(msg);
    }

    return tablelist;
  }

  /**
   * 设置表格"结构"内容
   * @author C.c
   * @since 2022-08-16 16:29
   * @param
   * @return
  **/
  public static String [][] setTableStructContent(List<ShowTableDto> tablelist,String[] title){

    String [][] content = new String[tablelist.size()][title.length];

    for (int i = 0; i < tablelist.size(); i++) {
      ShowTableDto showTableDto = tablelist.get(i);

      content[i][0] = showTableDto.getColumnComment()==null?null:showTableDto.getColumnComment();
      content[i][1] = showTableDto.getColumnName();
      content[i][2] = showTableDto.getColumnType();
      content[i][3] = showTableDto.getCharacterMaximumLength()==null?null:showTableDto.getCharacterMaximumLength();
    }
    return content;
  }
}
